<?php
class My_DashboardController extends MM_Controller_My_Action
{	
	public function indexAction(){
		try {
			//$this->pageview->assign("edata",$this->getdata($this->uid));
			$this->helper->setPageView($this->pageview, WEB_TITLE, "dashboard", null);
		} catch (Exception $e) {
			var_dump($e);
		}
	}
	
	public function geteventdetailAction()
	{
		if ( $this->uid<1 || $this->eid<1) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$data = $this->getdata($this->uid,$this->eid);			
			$this->result = $this->returnResult(1,"geteventdetail",$data);
		}
		echo $this->encodeResult($this->result);
	}
	
	private function getdata($uid,$request_eid = null){
		$data = null;
		$event = new MM_Service_Admin_Event();
		$guest = new MM_Service_Admin_Guests();
		$photo = new MM_Service_Admin_EventFiles();
		$focus = new MM_Service_Admin_EventUser();
		$follow = new MM_Service_Admin_EventFollower();
		
		$condition = sprintf("uid = '%s'",$uid);
		$condition2 = sprintf("sourceid = '%s'",$uid);
		
		$order = "startdate desc";
		
		$r1 = $event->getCount($condition);	
		$r2 = $guest->getCount($condition);	
		$rr = $event->getEventIds($uid);	
		$eidArray = $rr->getData();
		
		$condition3 = sprintf("eid in (%s)",implode(",", $eidArray));
		$r3 = $photo->getCount($condition3);//TODO 获取所有eid 获取所有图片
		
		if($request_eid){
			$eid = $request_eid;//用户自定义活动id
			$r4 = $event->find($eid,true);
			if($r4->getCode()){
				$data['lastest_event'] = $r4->getData();
			}			
		}else{
			$r4 = $event->fetchAll(1,1,$condition,$order,true);
			
			$array = $r4->getData();
			$eid = $array[0]['eid'];
			if($eid){
				$data['lastest_event'] = $array[0]; 			//最新活动信息
			}else{//没有数据
				$data['lastest_event'] = null;
			}
		}
		
		if($eid){
			$condition4 = sprintf("eid = '%s'",$eid);
			$r5 = $focus->getCount($condition4);
			$r6 = $follow->getCount($condition4);
			$data['lastest_eventuser'] = $r5->getData(); 	//最新来宾信息
			$data['lastest_eventfollow'] = $r6->getData();  //最新关注信息
		}else{
			$data['lastest_eventuser'] = null;
			$data['lastest_eventfollow'] = null;
		}
		
		$data['event_count'] = $r1->getData(); 				//活动条目
		$data['guest_count'] = $r2->getData(); 				//嘉宾人数
		$data['photo_count'] = $r3->getData(); 				//图片库张数
		$data['eid'] = $eid;
		return $data;
	}
}